package com.ruoyi.basebill.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.OmsSalereturnbillentry;
import com.ruoyi.basebill.mapper.OmsSalereturnbillMapper;
import com.ruoyi.basebill.domain.OmsSalereturnbill;
import com.ruoyi.basebill.service.IOmsSalereturnbillService;

/**
 * OMS销售退货单Service业务层处理
 * 
 * @author ptt
 * @date 2023-02-10
 */
@Service
public class OmsSalereturnbillServiceImpl implements IOmsSalereturnbillService 
{
    @Autowired
    private OmsSalereturnbillMapper omsSalereturnbillMapper;

    @Autowired
    private OmsSalereturnbillServiceImpl salereturnbillService;

    /**
     * 查询OMS销售退货单
     * 
     * @param id OMS销售退货单主键
     * @return OMS销售退货单
     */
    @Override
    public OmsSalereturnbill selectOmsSalereturnbillById(Long id)
    {
        return omsSalereturnbillMapper.selectOmsSalereturnbillById(id);
    }

    public OmsSalereturnbill selectOmsSalereturnbillSummaryEntryById(Long id)
    {
        return omsSalereturnbillMapper.selectOmsSalereturnbillSummaryEntryById(id);
    }



    /**
     * 查询OMS销售退货单列表
     * 
     * @param omsSalereturnbill OMS销售退货单
     * @return OMS销售退货单
     */
    @Override
    public List<OmsSalereturnbill> selectOmsSalereturnbillList(OmsSalereturnbill omsSalereturnbill)
    {
        return omsSalereturnbillMapper.selectOmsSalereturnbillList(omsSalereturnbill);
    }

    @Override
    public List<OmsSalereturnbill> selectOmsSalereturnbillDetailList(OmsSalereturnbill omsSalereturnbill) {
        return omsSalereturnbillMapper.selectOmsSalereturnbillDetailList(omsSalereturnbill);
    }

    /**
     * 新增OMS销售退货单
     * 
     * @param omsSalereturnbill OMS销售退货单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOmsSalereturnbill(OmsSalereturnbill omsSalereturnbill)
    {
        int rows = omsSalereturnbillMapper.insertOmsSalereturnbill(omsSalereturnbill);
        salereturnbillService.insertOmsSalereturnbillentry(omsSalereturnbill);
        return rows;
    }

    /**
     * 修改OMS销售退货单
     * 
     * @param omsSalereturnbill OMS销售退货单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOmsSalereturnbill(OmsSalereturnbill omsSalereturnbill)
    {
        if (omsSalereturnbill.getOmsSalereturnbillentryList() != null && omsSalereturnbill.getOmsSalereturnbillentryList().size() != 0){
            omsSalereturnbillMapper.deleteOmsSalereturnbillentryByParentid(omsSalereturnbill.getId());
            salereturnbillService.insertOmsSalereturnbillentry(omsSalereturnbill);
        }
        return omsSalereturnbillMapper.updateOmsSalereturnbill(omsSalereturnbill);
    }


    public int updateOmsSalereturnbillHead(OmsSalereturnbill omsSalereturnbill)
    {
        return omsSalereturnbillMapper.updateOmsSalereturnbill(omsSalereturnbill);
    }


    /**
     * 批量删除OMS销售退货单
     * 
     * @param ids 需要删除的OMS销售退货单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSalereturnbillByIds(Long[] ids)
    {
        omsSalereturnbillMapper.deleteOmsSalereturnbillentryByParentids(ids);
        return omsSalereturnbillMapper.deleteOmsSalereturnbillByIds(ids);
    }

    /**
     * 删除OMS销售退货单信息
     * 
     * @param id OMS销售退货单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSalereturnbillById(Long id)
    {
        omsSalereturnbillMapper.deleteOmsSalereturnbillentryByParentid(id);
        return omsSalereturnbillMapper.deleteOmsSalereturnbillById(id);
    }

    /**
     * 新增OMS销售退货单分录信息
     * 
     * @param omsSalereturnbill OMS销售退货单对象
     */
    public void insertOmsSalereturnbillentry(OmsSalereturnbill omsSalereturnbill)
    {
        List<OmsSalereturnbillentry> omsSalereturnbillentryList = omsSalereturnbill.getOmsSalereturnbillentryList();
        Long id = omsSalereturnbill.getId();
        if (StringUtils.isNotNull(omsSalereturnbillentryList))
        {
            List<OmsSalereturnbillentry> list = new ArrayList<OmsSalereturnbillentry>();
            for (OmsSalereturnbillentry omsSalereturnbillentry : omsSalereturnbillentryList)
            {
                omsSalereturnbillentry.setParentid(id);
                list.add(omsSalereturnbillentry);
            }
            if (list.size() > 0)
            {
                omsSalereturnbillMapper.batchOmsSalereturnbillentry(list);
            }
        }
    }
}
